﻿using AutoMapper;
using MediatR;
using SqlSugar;
using WarehouseManagement.Application.Command.AllocateCommand;
using WarehouseManagement.Domain.DTO.WarehouseSApp;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.AllocateCommandHandler
{
    /// <summary>
    /// 添加仓库调拨命令处理器
    /// </summary>
    public class AllocateCreateCommandHandler : IRequestHandler<AllocateCreateCommand, int>
    {
        private readonly IAllocateRepository _allocateRep;
        private readonly IAllocationProductsRepository _allocationProductsRep;
        private readonly lnterfaceStorageProductRepository _storageProductRep;
        private readonly lnterfaceWarehouseSAppRepository _warehouseSAppRep;
        protected readonly IMapper mapper;
        protected readonly ISqlSugarClient db;

        public AllocateCreateCommandHandler(IAllocateRepository allocateRep, IAllocationProductsRepository allocationProductsRep, lnterfaceStorageProductRepository storageProductRep, lnterfaceWarehouseSAppRepository warehouseSAppRep, IMapper mapper, ISqlSugarClient db)
        {
            this._allocateRep = allocateRep;
            this._allocationProductsRep = allocationProductsRep;
            this._storageProductRep = storageProductRep;
            this._warehouseSAppRep = warehouseSAppRep;
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<int> Handle(AllocateCreateCommand request, CancellationToken cancellationToken)
        {
            db.Ado.BeginTran();
            try
            {
                request.addDto.ANumber = Guid.NewGuid().ToString().Replace("-", "");
                var newout = mapper.Map<Allocate>(request.addDto);
                // 新增出库申请
                await _allocateRep.AddAsync(newout);
                foreach (var item in request.addDto.adddto)
                {
                    AllocationProducts orpr = new AllocationProducts()
                    {
                        AllocationProductsId = item.AllocationProductsId,
                        ANumber = newout.ANumber,
                        APAllocateNum = item.APAllocateNum,
                        APInboundWarehouse = item.APInboundWarehouse,
                        SPConfirmNum = item.APAllocateNum,
                        WName = item.WName,
                        PNumber = item.PNumber,
                        PName = item.PName,
                        PSpec = item.PSpec,
                        PUnit = item.PUnit,
                        AddName = newout.AddName,
                        AddTime = DateTime.Now,
                        Office_Id = newout.Office_Id,
                        User_Id = newout.User_Id,
                        IsDel = false,
                    };
                    await _allocationProductsRep.AddAsync(orpr);
                }

                //修改出库后的库存
                var inwarehouse = (await _storageProductRep.GetAll()).Where(t => t.PNumber == request.addDto.adddto[0].PNumber).ToList();
                inwarehouse.FirstOrDefault().SPConfirmNum -= (int)request.addDto.adddto[0].APAllocateNum;
                await _storageProductRep.UpdateAsync(inwarehouse.First());

                //修改入库的库存
                //var outwarehouse = (await _warehouseSAppRep.GetAll()).Where(t => t.WName == request.addDto.adddto[0].APInboundWarehouse).ToList();
                //outwarehouse.FirstOrDefault().SPConfirmNum += (int)request.addDto.adddto[0].APAllocateNum;
                //await _storageProductRep.UpdateAsync(outwarehouse);


                db.Ado.CommitTran();
                return 1;
            }
            catch (Exception)
            {
                db.Ado.RollbackTran();
                throw;
            }
        }
    }
}
